package com.zego.wawaji.ui.bean.user;

import android.os.Parcel;
import android.text.TextUtils;

import com.zego.wawaji.base.module.ModuleMgr;
import com.zego.wawaji.ui.bean.user.newuser.SevenRed;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户基本信息
 */
public class UserInfo extends UserBasic {
    private String avatarBig;
    private int avatar_status;   // 头像状态 0为未审核，1为通过，2为拒绝,3未上传 4 好 5 很好

    private int wwCoinCz;  // 充值的娃娃币
    private int wwCoinZs;  // 赠送的娃娃币
    private int wwNum;     // 拥有的娃娃数
    private int wwSendNum;  // 已发货的娃娃数
    private int wwPoint;   // 用户积分
    private int catchNum;  // 第一次抓中前抓的次数

    private int totalwwCoin; // 本地维护，不在解析列表中

    private int vipRank;   // vip等级， >0 极为vip
    private long vipEndTimeInt; // vip剩余时间（单位：s）

    private double amount;   // 用户充值金额
    private String firstRechargeTime;  // 用户首冲时间

    private int awardWwId;  // 首充开宝箱随机娃娃id: 未充值前不会有这个id
    private int boxStatus = -1;  // 宝箱状态， -1： 不可打开  1： 可打开  2：打开过的
    private List<SevenRed> redList = new ArrayList<>();

    public void parseBasic(String s) {
        super.parseJson(s);
    }

    @Override
    public void parseJson(String s) {
        JSONObject detailObject = getJsonObject(s);
        this.setUid(detailObject.optLong("Id"));
        this.setAvatarBig(detailObject.optString("AvatarBig"));
        this.setAvatar_status(detailObject.optInt("AvatarStatus"));

        this.setWwCoinCz(detailObject.optInt("WwCoinCz"));
        this.setWwCoinZs(detailObject.optInt("WwCoinZs"));
        this.setWwNum(detailObject.optInt("WwNum"));
        this.setWwSendNum(detailObject.optInt("WwSendNum"));
        this.setWwPoint(detailObject.optInt("WwPoint"));
        this.catchNum = detailObject.optInt("CatchNum");

        this.setTotalWwCoin(getWwCoinCz() + getWwCoinZs());

        this.vipRank = detailObject.optInt("VipRank");
        this.vipEndTimeInt = detailObject.optLong("VipEndTimeInt");
        this.firstRechargeTime = detailObject.optString("FirstRechargeTime");
        this.amount = detailObject.optDouble("Amount");

        String firstRechargeJson = detailObject.optString("FirstRechargeJson");
        if (!TextUtils.isEmpty(firstRechargeJson)) {
            JSONObject awardObject = getJsonObject(firstRechargeJson);
            this.awardWwId = awardObject.optInt("AwardWwId");
            this.boxStatus = awardObject.optInt("BoxStatus");

            String reds = awardObject.optString("Reds");
            if (!TextUtils.isEmpty(reds)) {
                JSONArray jsonArray = getJsonArray(reds);
                this.redList = (List<SevenRed>) getBaseDataList(jsonArray, SevenRed.class);
            }
        }
    }

    public int getCatchNum() {
        return catchNum;
    }

    public int getAwardWwId() {
        return awardWwId;
    }

    public int getBoxStatus() {
        return boxStatus;
    }

    public List<SevenRed> getRedList() {
        return redList;
    }

    public double getAmount() {
        return amount;
    }

    public String getFirstRechargeTime() {
        return firstRechargeTime;
    }

    public boolean isVip() {
        return vipRank > 0;
    }

    public long getVipEndTimeInt() {
        return vipEndTimeInt;
    }

    public String getVipEndString() {
        long currentTime = ModuleMgr.getAppMgr().getTime();
        long distance = vipEndTimeInt - currentTime / 1000;
        String ret = "";
        // 刷新列表
        if (distance <= 0) {

        } else if (distance < 60) {
            ret = "(剩余" + distance + "秒)";
        } else if (distance < 60 * 60) {
            distance = distance / 60;
            ret = "(剩余" + distance + "分钟)";
        } else if (distance < 60 * 60 * 24) {
            distance = distance / (60 * 60);
            ret = "(剩余" + distance + "小时)";
        } else {
            distance = distance / (60 * 60 * 24);
            ret = "(剩余" + distance + "天)";
        }
        return ret;
    }

    /**
     * 获取总娃娃币数量
     */
    public int getTotalWwCoin() {
        return totalwwCoin;
    }

    public void setTotalWwCoin(int wwCoin) {
        this.totalwwCoin = wwCoin;
    }

    public int getWwCoinCz() {
        return wwCoinCz;
    }

    public void setWwCoinCz(int wwCoinCz) {
        this.wwCoinCz = wwCoinCz;
    }

    public int getWwCoinZs() {
        return wwCoinZs;
    }

    public void setWwCoinZs(int wwCoinZs) {
        this.wwCoinZs = wwCoinZs;
    }

    public int getWwNum() {
        return wwNum;
    }

    public void setWwNum(int wwNum) {
        this.wwNum = wwNum;
    }

    public int getWwSendNum() {
        return wwSendNum;
    }

    public void setWwSendNum(int wwSendNum) {
        this.wwSendNum = wwSendNum;
    }

    public int getWwPoint() {
        return wwPoint;
    }

    public void setWwPoint(int wwPoint) {
        this.wwPoint = wwPoint;
    }

    public int getAvatar_status() {
        return avatar_status;
    }

    public void setAvatar_status(int avatar_status) {
        this.avatar_status = avatar_status;
    }

    public String getAvatarBig() {
        return avatarBig;
    }

    public void setAvatarBig(String avatarBig) {
        this.avatarBig = avatarBig;
    }

    public UserInfo() {
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.avatarBig);
        dest.writeInt(this.avatar_status);
        dest.writeInt(this.wwCoinCz);
        dest.writeInt(this.wwCoinZs);
        dest.writeInt(this.wwNum);
        dest.writeInt(this.wwSendNum);
        dest.writeInt(this.wwPoint);
        dest.writeInt(this.catchNum);
        dest.writeInt(this.totalwwCoin);
        dest.writeInt(this.vipRank);
        dest.writeLong(this.vipEndTimeInt);
        dest.writeDouble(this.amount);
        dest.writeString(this.firstRechargeTime);
        dest.writeInt(this.awardWwId);
        dest.writeInt(this.boxStatus);
        dest.writeTypedList(this.redList);
    }

    protected UserInfo(Parcel in) {
        super(in);
        this.avatarBig = in.readString();
        this.avatar_status = in.readInt();
        this.wwCoinCz = in.readInt();
        this.wwCoinZs = in.readInt();
        this.wwNum = in.readInt();
        this.wwSendNum = in.readInt();
        this.wwPoint = in.readInt();
        this.catchNum = in.readInt();
        this.totalwwCoin = in.readInt();
        this.vipRank = in.readInt();
        this.vipEndTimeInt = in.readLong();
        this.amount = in.readDouble();
        this.firstRechargeTime = in.readString();
        this.awardWwId = in.readInt();
        this.boxStatus = in.readInt();
        this.redList = in.createTypedArrayList(SevenRed.CREATOR);
    }

    public static final Creator<UserInfo> CREATOR = new Creator<UserInfo>() {
        @Override
        public UserInfo createFromParcel(Parcel source) {
            return new UserInfo(source);
        }

        @Override
        public UserInfo[] newArray(int size) {
            return new UserInfo[size];
        }
    };
}
